<template>
  <div>
    <!-- 顾客管理 -->
    <!-- 表单控件 -->
    <div class="header">
      <el-form :inline="true" :model="params" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary" @click="toAdd">添加</el-button>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="params.username"
            clearable
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item>
          <el-select v-model="params.status" clearable placeholder="请选择状态">
            <el-option label="禁用" value="禁用" />
            <el-option label="正常" value="正常" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表单控件结束 -->
    <!-- 表格 -->
    <el-table :data="tabledata" style="width: 100%">
      <el-table-column prop="id" label="编号" width="180" />
      <el-table-column prop="username" label="用户名" width="180" />
      <el-table-column prop="realname" label="姓名" />
      <el-table-column prop="telephone" label="手机号" />
      <el-table-column prop="gender" label="性别" />
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.status === '正常'"
            type="success"
          >正常</el-tag>
          <el-tag v-if="scope.row.status === '禁用'" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <a href="#" class="toEdit" @click.prevent="toEdit(scope.row)">编辑</a>
          <a
            href="#"
            class="toremove"
            @click.prevent="toremove(scope.row.id)"
          >删除</a>
          <a
            href="#"
            class="toDetails"
            @click.prevent="toDetails(scope.row.id)"
          >详情</a>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 分页按钮 -->
    <div class="page">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[5, 10, 20, 30, 50]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 分页按钮结束 -->
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <el-form ref="myForm" :model="form" :rules="rules">
        <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="realname">
          <el-input v-model="form.realname" autocomplete="off" />
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth" prop="telephone">
          <el-input v-model="form.telephone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-radio-group v-model="gender">
            <el-radio label="男" />
            <el-radio label="女" />
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="condition" label="状态" :label-width="formLabelWidth" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="禁用" />
            <el-radio label="正常" />
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toClose">取 消</el-button>
        <el-button type="primary" @click="toSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import { saveOrUpdate, pageQuery, deleteById } from '../../../api/user'

export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10
      },
      tabledata: [],
      total: 0,
      gender: '',
      // 模态框显示与隐藏
      visible: false,
      title: '',
      formLabelWidth: '120px',
      // 表单对象
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        realname: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        telephone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        status: [{ required: true, trigger: 'change' }]
      },
      condition: false
    }
  },
  computed: {},
  created() {
    this.getTabledata()
  },
  mounted() {},
  methods: {
    // 关闭
    toClose() {
      // 重置表单
      setTimeout(() => {
        this.$refs.myForm.resetFields()
        this.visible = false
      }, 500)
    },
    // 更新、保存
    toSubmit() {
      // 校验表单
      this.$refs.myForm.validate(async(valid) => {
        if (valid) {
          // 发送保存或者更新请求
          const res = await saveOrUpdate(this.form)
          this.$notify({
            type: 'success',
            message: res.message
          })
          this.getTabledata()
          this.toClose()
        } else {
          return false
        }
      })
    },
    // 添加
    toAdd() {
      this.visible = true
      this.title = '添加顾客信息'
      this.condition = false
      this.form = {}
    },
    // 查询
    onSearch() {
      this.params.page = 1
      this.getTabledata()
    },
    // 编辑
    toEdit(row) {
      console.log(row)
      this.visible = true
      this.title = '编辑顾客信息'
      this.form = { ...row }
      this.condition = true
    },
    // 删除
    toremove(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteById({ id: id })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getTabledata()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 去详情
    toDetails(id) {
      this.$router.push({
        path: 'Details',
        query: {
            	id: id
        }
      })
    },
    async getTabledata() {
      // 数据加载前加载动画
      this.loading = true
      for (const key in this.params) {
        // key 属性名
        // this.params[key] 属性值
        if (!this.params[key]) {
          delete this.params[key]
        }
      }
      const res = await pageQuery(this.params)
      // console.log(res);
      this.tabledata = res.data.list
      this.total = res.data.total
      if (this.tabledata.gender === 'male') {
        this.gender = '男'
      } else {
        this.gender = '女'
      }
      // 关闭加载动画
      this.loading = false
    },
    // 页码条数发生改变的方法 默认参数pageSize
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize
      this.params.page = 1
      this.getTabledata()
    },
    // 当前页数发生改变触发的方法 默认参数page
    handleCurrentChange(page) {
      this.params.page = page
      this.getTabledata()
    }
  }
}
</script>
<style lang="scss">
.toEdit {
  color: #30b08f;
}
.toremove {
  color: #e65d6e;
}
.toDetails {
  color: #3a71a8;
}
</style>
